<template>
  <!--
      作者：luoyiming
      时间：2020-06-01
      描述：插件中心-文章-添加
    -->
  <div class="product-add pb50">

    <!--添加文章-->
    <el-form size="small" :model="form" ref="form" :rules="rules" label-width="100px">
      <el-tabs v-model="activeName">
        <el-tab-pane label="常规选项" name="0">
          <template>
            <div class="common-form">添加内容</div>
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" class="max-w460"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="desc">
              <el-input type="textarea" rows="3":maxlength="100" show-word-limit  v-model="form.desc" placeholder="请输入描述" class="max-w460"></el-input>
            </el-form-item>
            <el-form-item label="所属栏目">
              <el-select size="small" v-model="form.menu_id" placeholder="所有栏目">
                <el-option v-for="(item, index) in menu" :key="index" :label="item.name" :value="item.menu_id"
                  :disabled="item.disabled">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="缩略图">
              <div>
                <el-button type="primary" @click="openProductUpload('image')">上传图片</el-button>
                <img class="mt10" width="120px;" v-img-url="form.image" v-if="form.image" />
              </div>
            </el-form-item>
            <el-form-item label="图集：">
              <div class="draggable-list">
                <draggable class="wrapper" v-model="form.images">
                  <transition-group>
                    <div class="item" v-for="(item, index) in form.images" :key="'n'+index">
                      <img v-img-url="item.file_path" />
                      <a href="javascript:void(0);" class="delete-btn" @click.stop="deleteImg(index)"><i
                          class="el-icon-close"></i></a>
                    </div>
                  </transition-group>
                </draggable>
                <div class="item img-select" @click="openProductUpload('images')"><i class="el-icon-plus"></i></div>
              </div>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="form.status">
                <el-radio :label="1">显示</el-radio>
                <el-radio :label="0">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="内容详情">
              <div class="edit_container">
                <Uediter v-model="form.content" :text="ueditor.text" :config="ueditor.config" ref="ue"></Uediter>
              </div>
            </el-form-item>
            <el-form-item label="排序">
              <el-input type="number" v-model="form.sort" placeholder="请输入数字" class="max-w460"></el-input>
            </el-form-item>
          </template>
        </el-tab-pane>
        <el-tab-pane label="SEO选项" name="1">
          <el-form-item label="SEO标题" prop="seo_title">
            <el-input v-model="form.seo_title" placeholder="请输入SEO标题" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="SEO关键词" prop="seo_keywords">
            <el-input v-model="form.seo_keywords" type="textarea" :rows="2" placeholder="请输入SEO关键词" class="max-w460">
            </el-input>
          </el-form-item>
          <el-form-item label="SEO描述" prop="seo_description">
            <el-input v-model="form.seo_description" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入SEO描述" class="max-w460"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="其他选项" name="2">
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" placeholder="请输入作者" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="来源" prop="origin">
            <el-input v-model="form.origin" placeholder="请输入来源" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="浏览量" prop="click">
            <el-input v-model="form.click" placeholder="请输入浏览量" class="max-w460"></el-input>
          </el-form-item>
          <el-form-item label="发布时间" prop="release_time">
            <el-date-picker v-model="form.release_time" type="datetime" placeholder="请选择发布时间"></el-date-picker>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>

      <!--提交-->
      <div class="common-button-wrapper">
        <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
      </div>

    </el-form>
    <!--上传图片组件-->
    <Upload v-if="isupload" :config="config" :isupload="isupload" @returnImgs="returnProductImgsFunc">上传图片</Upload>
  </div>
</template>

<script>
  import InfoApi from '@/api/info.js';
  import Uediter from '@/components/UE.vue';
  import Upload from '@/components/file/Upload';
  import draggable from 'vuedraggable';
  export default {
    components: {
      /*编辑器*/
      Uediter,
      /*图片上传*/
      Upload: Upload,
      draggable
    },
    data() {
      return {
        config: {},
        activeName: '0',
        /*是否上传图片*/
        isupload: false,
        isImg: false,
        /*富文本配置*/
        ueditor: {
          text: '',
          config: {
            initialFrameWidth: 800,
            initialFrameHeight: 500
          }
        },
        path: '',
        /*form表单数据*/
        form: {
          title: '',
          image: '',
          sort: 1,
          status: 1,
          content: '',
          desc: '',
          menu_id: 0,
          images: [],
          seo_title: '',
          seo_keywords: '',
          seo_description: '',
          author: '',
          origin: '',
          release_time: ''
        },
        /*栏目*/
        menu: [],
        loading: false,
        /*验证规则*/
        rules: {
          title: [{
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }],
        },
        menu_id: 0,
        file_name: ''
      };
    },
    created() {
      console.log(this.form.images)
      this.menu_id = this.$route.query.menu_id;
      this.form.menu_id = this.$route.query.menu_id * 1;
      /*获取列表*/
      this.getMenuList();
      this.form.release_time = new Date();
    },
    methods: {
      /*获取栏目*/
      getMenuList() {
        let self = this;
        let Params = {};
        Params.menu_id = this.menu_id;
        InfoApi.toAddInfo(Params, true)
          .then(res => {
           self.menu = res.data.menu;
          })
          .catch(error => {

          });
      },

      /*添加文章*/
      onSubmit() {
        let self = this;
        let form = self.form;
        self.form.content = this.$refs.ue.getUEContent();
        self.$refs.form.validate(valid => {
          if (valid) {
            self.loading = true;
            InfoApi.addInfo(form, true)
              .then(data => {
                self.loading = false;
                self.$message({
                  message: data.msg,
                  type: 'success'
                });
                self.$router.push('/info/info/index');
              })
              .catch(error => {
                self.loading = false;
              });
          }
        });
      },
      /*打开上传图片*/
      openProductUpload: function(file_name) {
        this.file_name = file_name;
        if (file_name == 'images') {
          this.config = {
            total: 9,
            file_type: 'image'
          };
        } else {
          this.config = {
            total: 1,
            file_type: 'image'
          };
        }
        console.log(this.config)
        this.isupload = true;
      },

      /*上传商品图片*/
      returnProductImgsFunc(e) {
        console.log(e)
        if (e != null) {
          if (this.file_name == 'images') {
            let imgs = this.form.images.concat(e);
            this.$set(this.form, 'images', imgs);
          } else if (this.file_name == 'image') {
            this.form.image = e[0].file_path;;
          }

        }
        this.isupload = false;
      },

      /*删除商品图片*/
      deleteImg(index) {
        this.form.images.splice(index, 1);
      },
      /*取消添加，返回文章列表*/
      cancelFunc() {
        this.$router.push({
          path: '/info/info/index'
        });
      }
    }
  };
</script>

<style>
  .edit_container {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
  }

  .ql-editor {
    height: 400px;
  }

  .draggable-list {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .draggable-list .wrapper>span {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .draggable-list .item {
    position: relative;
    width: 110px;
    height: 110px;
    margin-top: 10px;
    margin-right: 10px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #dddddd;
  }

  .draggable-list .delete-btn {
    position: absolute;
    top: 0;
    right: 0;
    width: 16px;
    height: 16px;
    background: red;
    line-height: 16px;
    font-size: 16px;
    color: #ffffff;
    display: none;
  }

  .draggable-list .item:hover .delete-btn {
    display: block;
  }

  .draggable-list .item img {
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    max-height: 100%;
    max-width: 100%;
  }

  .draggable-list .img-select {
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px dashed #dddddd;
    font-size: 30px;
  }

  .draggable-list .img-select i {
    color: #409eff;
  }
</style>
